<?php

namespace App\Model;

use Illuminate\Database\Eloquent\Model;

class Staff_task extends Model
{
    //
	protected $table='staff_task';
	protected $fillable=['task_id','staff_id','store_id','receive_time','status','store_station_id','is_agree','termination'];
	protected $hidden=[];


//	查询任务执行人
	public function allimplement($store_id,$task_id){
		return $this->where(['store_id'=>$store_id,'status'=>1,'task_id'=>$task_id])->with('staff_info')->get();
	}

	public function staff_info(){
		return $this->belongsTo('App\Model\Staff','staff_id');
	}


//	终止结算详情
	public function termination_desc($id){
		$data= $this->where(['id'=>$id])->with(['task'=>function($query){
			$query->with('store_station');
		}])->first();
		$data['type']=$data['task']['store_station']['status']==0?'短期有效':'长期有效';
		return $data;
	}


//	任务详情
	public function task(){
		return $this->belongsTo('App\Model\Task','task_id')->with('store_station');
	}

//	终止结算列表
	public function stop_list($store_id){
		$data=$this->where(function ($query) use($store_id){
			$query->where(['store_id'=>$store_id]);
			$query->whereNotNull('termination');
			$query->whereNull('is_agree');
		})->with('staffInfo')->with('store_station')->get();
		return $data;
	}


//	员工信息
	public function staffInfo(){
		return $this->belongsTo('App\Model\Staff','staff_id');
	}

//	店铺岗位信息
	public function store_station(){
		return $this->belongsTo('App\Model\Store_station','store_station_id');
	}

//	查询历史执行人
	public function history_staff($where){
		$Arr=[];
		$data=$this->where(['store_id'=>$where['store_id'],'status'=>6])
			->whereBetween('receive_time',[$where['start_time'],$where['end_time']])
			->with('staffInfo')->with('store_station')->get()->toArray();
		$newArr=[];
		foreach($data as $kk=>$vv){
			$newArr[$vv['store_station_id']]['data'][]=$vv;
			$newArr[$vv['store_station_id']]['station_name']=$vv['store_station']['name'];
		}
		foreach($newArr as $key=>$value){
			$Arr[]=$value;
		}
		return $Arr;
	}


//	获取排班人员列表
	public function staff_list($store_id){

		 $data=$this->where(['store_id'=>$store_id])
			 ->where(function ($query) {
				 $query->where(['status'=>3])->orwhere(function ($query){
				 	$query->whereNull('receive_time');
				 });
			 })->with('store_station')->with('staffInfo')->get();
		return $data;
	}



//	查询历史任务
	public function history_task($where){
		$data=$this->where(['uid'=>$where['uid'],'status'=>$where['status']])
			->whereBetween('created_at',[$where['start_time'],$where['end_time']])
			->with('task')->get();
		return $data;
	}

//	查询历史任务详情
	public function history_task_desc($id){
		$data=$this->where(['id'=>$id])->with('task')->first();
		return $data;
	}

}
